package com.bw.educate.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.educate.common.ResponseResult;
import com.bw.educate.common.auth.UserInfo;
import com.bw.educate.entity.EduSectionNode;
import com.bw.educate.service.IEduSectionNodeService;
import com.bw.educate.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 在线教育-五组
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/nodes")
public class EduSectionNodeController {

    @Autowired
    private IEduSectionNodeService sectionNodeService;

    @Autowired
    private UserUtil userUtil;

    @PostMapping("savenotes")
    public ResponseResult savenotes(@RequestBody EduSectionNode sectionNode, HttpServletRequest request){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            UserInfo loginUser = userUtil.getLoginUser(request);
            sectionNode.setUserId(loginUser.getId());
            sectionNode.setUpdateTime(new Date());
            sectionNode.setStatus(0);
            sectionNodeService.saveOrUpdate(sectionNode);
            result.setMessage("保存成功");
        }catch (Exception e){
            result=ResponseResult.FAIL();
        }
        return result;
    }
    @GetMapping("list/{uid}")
    public ResponseResult list(@PathVariable("uid") long uid){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("USER_ID",uid);
            List list = sectionNodeService.list(queryWrapper);
            result.setResult(list);
            result.setMessage("保存成功");
        }catch (Exception e){
            result=ResponseResult.FAIL();
        }
        return result;
    }
    @GetMapping("getnodes/{jid}")
    public ResponseResult getnodes(@PathVariable("jid") long jid, HttpServletRequest request){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            UserInfo loginUser = userUtil.getLoginUser(request);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("USER_ID",loginUser.getId());
            queryWrapper.eq("SECTION_ID",jid);
            EduSectionNode node = sectionNodeService.getOne(queryWrapper);
            result.setResult(node);
            result.setMessage("保存成功");
        }catch (Exception e){
            result=ResponseResult.FAIL();
        }
        return result;
    }
}
